//app.js
const log = require('./utils/log.js');
App({
  onLaunch: function () {
    console.log("aaa");
    var that = this;
    that.globalData.isLogin = wx.getStorageSync("isLogin");
    that.globalData.openId = wx.getStorageSync("isLogin");
    that.globalData.openKey = wx.getStorageSync("openKey");
    that.globalData.openIdPay = wx.getStorageSync("openIdPay");
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs);
    log.info('app.js里面的日志会到打到首页');
  },
  onShow:function(){
    var that=this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.model)
        console.log(res.platform)
        // 判断手机系统
        if (res.platform == "ios") {
          that.globalData.isAndroidPlatform = false
        } else if (res.platform == "android") {
          that.globalData.isAndroidPlatform = true
        }
      }
    })
    log.info('登录：' + that.globalData.isLogin)
  },
  globalData: {
    zcyAppID: '411', //平台APP ID
    platformId: '210',//平台platform_id
    cengJiNode:'2732',//云平台层级节点
    userInfo: null,
    isLogin:false,
    openId:"", //
    openKey:'',
    deviceId:'',
    nickname:'',
    orderID: '',
    telephone: '',
    oldPassword:'',
    deviceName:'',
    openIdPay: '',//微信用户标识
    unionid: '',//同一主体下微信用户标识
    connectedDeviceId: "", //已连接设备uuid
    isAndroidPlatform:'',//手机系统是否是安卓
    modeId: 'dhcnco', //产品型号  
    pin: 'ae55a440fe1a11e5bf8a5254007b2d50',//PIN码
    username:'',//登录名称
    APPCODE: '4002901a71c2485886cb7fedff911296',//天气预报APPcode
    wxAppID: 'wx82fde0d91f7a4f86',//小程序的APPID
    AppSecret:'eb416311b259b2bf3dcb4cdf2ee34fb3',//小程序秘钥
    logInCode: '',//wx.login获取的code
    isUserLocation:false,//是否授权获取地理位置
    wifiModeId: 'yypwvh',//
  },
  //   // ArrayBuffer转16进度字符串示例
  ab2hex(buffer) {
    const hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
        return ('00' + bit.toString(16)).slice(-2)
      }
    )
    return hexArr.join('')
  },
  hexStringToArrayBuffer: function (str) {
    if (!str) {
      return new ArrayBuffer(0);
    }
    var buffer = new ArrayBuffer(str.length);
    let dataView = new DataView(buffer)
    let ind = 0;
    for (var i = 0, len = str.length; i < len; i += 2) {
      let code = parseInt(str.substr(i, 2), 16)
      dataView.setUint8(ind, code)
      ind++
    }
    return buffer;
  },
  arrayBufferToHexString(buffer) {
    let bufferType = Object.prototype.toString.call(buffer)
    if(buffer != '[object ArrayBuffer]') {
      return
    }
    let dataView = new DataView(buffer)
    var hexStr = '';
    for (var i = 0; i < dataView.byteLength; i++) {
      var str = dataView.getUint8(i);
      var hex = (str & 0xff).toString(16);
      hex = (hex.length === 1) ? '0' + hex : hex;
      hexStr += hex;
    }
  // ****
    return hexStr.toUpperCase();
  },
  // userLogin: function () {
  //   let that = this;
  //   let promise = new Promise((resolve, reject) => {
  //     wx.login({
  //       success(res) {
  //         console.log(res.code);
  //         that.globalData.logInCode = res.code;
  //         wx.request({
  //           url: 'https://papi.zcyun.cn/v1/user/weixin/auth?code=' + that.globalData.logInCode + '&appId=' + that.globalData.wxAppID,
  //           method: "GET",
  //           success(res) {
  //             console.log(res);
  //             if (res.data.code == 0) {
  //               that.globalData.isLogin = true;
  //               that.globalData.openId = res.data.data.openId;
  //               that.globalData.openKey = res.data.data.openKey;
  //               that.globalData.openIdPay = res.data.data.openid;
  //               wx.setStorage({
  //                 key: 'openId',
  //                 data: res.data.data.openId,
  //                 success: function (res) { }
  //               })
  //               wx.setStorage({
  //                 key: 'openKey',
  //                 data: res.data.data.openKey,
  //                 success: function (res) { }
  //               })
  //               wx.setStorage({
  //                 key: 'isLogin',
  //                 data: true,
  //                 success: function (res) { }
  //               })
  //               wx.setStorage({
  //                 key: 'openIdPay',
  //                 data: res.data.data.openid,
  //                 success: function (res) { }
  //               })
  //             }
  //           },
  //           fail(err) {
  //             console.log(err)
  //           }
  //         })
  //       }
  //     })
  //   })
  //   return promise;
  // },
})
 